import openpyxl
import keyboard
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait

def in_sys():
      

      
      time.sleep(0.4)
      print("开始")
      if 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[1]/table/tbody/tr[1]/td[2]/span/input[1]').get_attribute('value') != sheet.cell(4,6).value.strftime('%Y-%m-%d'):
            for _ in range(12):
                  浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[1]/table/tbody/tr[1]/td[2]/span/input[1]').send_keys(Keys.BACKSPACE)  # 日期框
            print("输入日期")
            浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[1]/table/tbody/tr[1]/td[2]/span/input[1]').send_keys(sheet.cell(4,6).value.strftime('%Y-%m-%d'))  # 输入日期
            time.sleep(0.4)
            浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[1]/table/tbody/tr[1]/td[2]/span/input[1]').send_keys(Keys.ENTER)  # 日期框
      if 浏览器.find_element(By.XPATH, '//*[@id="Newfjcr"]').get_attribute('value') != sheet.cell(4,4).value:
            for _ in range(10):
                  浏览器.find_element(By.XPATH, '//*[@id="Newfjcr"]').send_keys(Keys.BACKSPACE)  # 清空检查人框
            print("输入检查人")
            浏览器.find_element(By.XPATH, '//*[@id="Newfjcr"]').send_keys(sheet.cell(4,4).value)  # 输入检查人
      if "道岔" in sheet.cell(4,9).value:
            print("道岔")
            浏览器.find_element(By.XPATH, '//*[@id="Newfjclx2"]').click()    #点击道岔
            if sheet.cell(4,10).value != 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[2]/div[1]/span/input[1]').get_attribute('value'):
                  浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[2]/div[1]/span/span/span').click()    #点击车站下拉菜单  
                  浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" + sheet.cell(4,10).value +"']",).click()                #选择车站
            if sheet.cell(4,11).value.split("#")[0] != 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[4]/div[2]/span/input[1]').get_attribute('value'):
                  浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[4]/div[2]/span/span/span').click()    #点击道岔下拉菜单
                  浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" + sheet.cell(4,11).value.split("#")[0] +"']",).click()                #选择道岔
            time.sleep(0.4)
            if sheet.cell(4,15).value is not None and len(str(sheet.cell(4,15).value)) > 0:
                  if sheet.cell(4,15).value != 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[6]/div[1]/span[1]/input[1]').get_attribute('value'):
                        浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[6]/div[1]/span[1]/span/span').click()    #点击道岔位置1下拉菜单
                        浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" + sheet.cell(4,15).value +"']",).click()                #选择道岔位置1
            if sheet.cell(4,16).value is not None and len(str(sheet.cell(4,16).value)) > 0:
                  if sheet.cell(4,16).value != 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[6]/div[1]/span[2]/input[1]').get_attribute('value'):
                        浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[6]/div[1]/span[2]/span/span').click()    #点击道岔位置2下拉菜单
                        浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" + sheet.cell(4,16).value +"']",).click()                #选择道岔位置2

      elif "股道" in sheet.cell(4,9).value:
            print("股道")
            time.sleep(0.4)
            浏览器.find_element(By.XPATH, '//*[@id="Newfjclx3"]').click()    #点击股道
            if sheet.cell(4,10).value != 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[2]/div[1]/span/input[1]').get_attribute('value'):
                  浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[2]/div[1]/span/span/span').click()    #点击车站
                  浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" +sheet.cell(4,10).value +"']",).click()                #选择车站
            if str(sheet.cell(4,11).value) != 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[4]/div[3]/span/input[1]').get_attribute('value'):
                  浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[4]/div[3]/span/span/span').click()    #点击股道下拉菜单
                  浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" +str(sheet.cell(4,11).value) +"']",).click()                #选择股道
            for _ in range(10):
                  浏览器.find_element(By.XPATH, '//*[@id="Newfth"]').send_keys(Keys.BACKSPACE)  # 清空铁号
            print("股道结束")
      else:
            print("正线")
            浏览器.find_element(By.XPATH, '//*[@id="Newfjclx1"]').click()    #点击正线
            if str(sheet.cell(4,9).value) != 浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[2]/div[2]/span/input[1]').get_attribute('value'):
                  浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[2]/table/tbody/tr[1]/td[2]/div[2]/span/span/span').click()    #行别
                  浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" +sheet.cell(4,9).value +"']",).click()                #点击行别
            for _ in range(10):
                  浏览器.find_element(By.XPATH, '//*[@id="Newflc"]').send_keys(Keys.BACKSPACE)  # 清空检查人框
            浏览器.find_element(By.XPATH, '//*[@id="Newflc"]').send_keys(str(sheet.cell(4,10).value) + "." + str(sheet.cell(4,11).value ))  # 输入起始里程
      print("病害内容开始")
      浏览器.find_element(By.XPATH, '//*[@id="wzxxBtn"]/span/span/span',).click()                #点击✔
      for _ in range(30):
            浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[3]/table/tbody/tr[2]/td[6]/span/input[1]').send_keys(Keys.BACKSPACE)  # 清空病害情况
      浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[3]/table/tbody/tr[2]/td[6]/span/input[1]').send_keys(str(sheet.cell(4,18).value) + str(sheet.cell(4,17).value))  # 输入病害情况
      time.sleep(0.4)
      浏览器.find_element(By.XPATH, '//*[@id="btnbhfltree"]/span/span/span',).click()                #点击病害内容+
      time.sleep(0.4)
      for _ in range(10):
            浏览器.find_element(By.XPATH, '//*[@id="s_name"]').send_keys(Keys.BACKSPACE)  # 清空偏差类型/病害项目
      浏览器.find_element(By.XPATH, '//*[@id="s_name"]').send_keys(sheet.cell(4,14).value)  # 偏差类型/病害项目
      for _ in range(10):
            浏览器.find_element(By.XPATH, '//*[@id="Newfcxfz"]').send_keys(Keys.BACKSPACE)  # 清空输入峰值
      浏览器.find_element(By.XPATH, '//*[@id="Newfcxfz"]').send_keys(sheet.cell(4,17).value)  # 输入峰值
      print('等待手动点击病害类型后按F2继续：')
      keyboard.wait('f2')
      for _ in range(10):
            浏览器.find_element(By.XPATH, '//*[@id="Newfcxfz"]').send_keys(Keys.BACKSPACE)  # 清空输入峰值
      浏览器.find_element(By.XPATH, '//*[@id="Newfcxfz"]').send_keys(sheet.cell(4,17).value)  # 输入峰值
      for _ in range(30):
            浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[3]/table/tbody/tr[2]/td[6]/span/input[1]').send_keys(Keys.BACKSPACE)  # 清空病害情况
      浏览器.find_element(By.XPATH, '//*[@id="TjtjcCxxxForm"]/fieldset[3]/table/tbody/tr[2]/td[6]/span/input[1]').send_keys(str(sheet.cell(4,18).value) + str(sheet.cell(4,17).value))  # 输入病害情况
      
      print('请在手动填写是否重复及作业项目后，按F2，按F2后将自动保存：')
      keyboard.wait('f2')
      while 1:
            if "(" in 浏览器.find_element(By.XPATH, '//*[@id="NewzyxmList"]').get_attribute('value'):
                  st_r = 浏览器.find_element(By.XPATH, '//*[@id="NewzyxmList"]').get_attribute('value')           #获取当前作业项目
                  print(len(浏览器.find_element(By.XPATH, '//*[@id="Newfzzcs"]').get_attribute('value')))
                  if len(浏览器.find_element(By.XPATH, '//*[@id="Newfzzcs"]').get_attribute('value'))< 2:
                        浏览器.find_element(By.XPATH, '//*[@id="Newfzzcs"]').send_keys(st_r.split("(")[0])       #自动根据作业项目填写整治措施
                  if len(浏览器.find_element(By.XPATH, '//*[@id="Newfzzcs"]').get_attribute('value')) > 0:
                        浏览器.find_element(By.XPATH, '//*[@id="NewSubCxx"]/span/span',).click()                #点击保存
                        break
            else:
                  print("请填写作业项目，请选择作业项目后按F2继续：")
                  keyboard.wait('f2')
      print("保存成功点击确定然后按F2继续：")
      keyboard.wait('f2')
      sheet.delete_rows(4)
      workbook.save("病害库.xlsx")
      

workbook = openpyxl.load_workbook("病害库.xlsx")
sheet = workbook['病害库']
if sheet.max_row > 3 and sheet.cell(4,2).value  is not None:
      opt=Options()
      opt.binary_location =r'D:\chrome-win32\chrome.exe'
      浏览器 = webdriver.Chrome( executable_path =r'D:\chrome-win32\chromedriver.exe',options=opt)  # 创建浏览器对象
      wait = WebDriverWait(浏览器,60)
      浏览器.get('http://10.190.168.59/AqscWebMIS/')  # 打开工务安全上次管理系统网页
      浏览器.implicitly_wait(60)
      浏览器.find_element(By.XPATH, '//*[@id="usrCode"]').send_keys(sheet.cell(2,23).value)  # 输入用户名
      浏览器.find_element(By.XPATH, '//*[@id="usrPass"]').send_keys(sheet.cell(2,24).value)  # 输入密码
      浏览器.find_element(By.XPATH, '//*[@id="loginBtn"]').click()  # 点击登录
      浏览器.switch_to.frame(浏览器.find_element(By.XPATH, '//*[@id="frameTopMainBody"]'))  # 切换到主框架
      浏览器.find_element(By.XPATH, '//*[@id="divcorner_aroot_0"]/a').click()  # 点击检测管理
      浏览器.switch_to.frame(浏览器.find_element(By.CSS_SELECTOR, 'iframe#mainBot'))  # 切换到mainBot框架
      time.sleep(0.4)
      浏览器.find_element(By.XPATH, '//*[@id="menu_3_1"]').click()    #点击人工检查上报
      浏览器.switch_to.frame(浏览器.find_element(By.CSS_SELECTOR, '#ifm_1'))  # 切换到ifm_1框架
      time.sleep(0.2)
      print("线名",浏览器.find_element(By.CSS_SELECTOR, "body > table > tbody > tr.tr_interlace1 > td > table > tbody > tr > td:nth-child(4) > span > input.combo-text.validatebox-text").get_attribute('value'))
      if sheet.cell(4,7).value != 浏览器.find_element(By.XPATH, "/html/body/table/tbody/tr[1]/td/table/tbody/tr/td[4]/span/input[1]").get_attribute('value'):
            浏览器.find_element(By.XPATH, '/html/body/table/tbody/tr[1]/td/table/tbody/tr/td[4]/span/span/span').click()    #点击线名下拉菜单
            time.sleep(0.4)
            浏览器.find_element(By.XPATH, "//div[@class='combobox-item' and text()='" +sheet.cell(4,7).value +"']").click()
      浏览器.switch_to.frame(浏览器.find_element(By.CSS_SELECTOR, '#frmturl'))  # 切换到frmturl框架
      浏览器.find_element(By.XPATH, '//*[@id="gtbutton2"]/span/span').click()    #点击病害录入
      while 1 :
            if sheet.cell(4,2).value  is not None:
                  in_sys()
            
       
